package com.example.easeimpro.model.db;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.easeimpro.model.bean.GroupInfo;
import com.example.easeimpro.model.bean.InviteInfo;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.dao.InviteTableDao;
import com.example.easeimpro.utils.SpUtils;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMGroupChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMucSharedFile;
import com.hyphenate.easeui.interfaces.EaseGroupListener;

import java.util.List;

/**
 * 全局事件监听类
 */
public class EventListener {
    private Context mContext;
    private LocalBroadcastManager instance;
    private static final String TAG = "EventListener";

    public EventListener(Context context) {
        mContext = context;
        instance = LocalBroadcastManager.getInstance(context);
        //注册一个联系人变化的监听
        EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
            @Override
            public void onContactAdded(String hxId) {
                Log.e(TAG, "onContactAdded: 联系人添加");
                //联系人添加
                //数据更新
                Model.getInstance().getDbManager().getContactTableDao().saveContact(new UserInfo(hxId),true);
                InviteInfo info = new InviteInfo();
                info.setReason("同意添加好友");
                Model.getInstance().getDbManager().getInviteTableDao().updateInviteReason(info,hxId);
                //发送联系人变化广播
                instance.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
            }

            @Override
            public void onContactDeleted(String username) {
                //联系人删除
                Log.e(TAG, "onContactDeleted: 联系人删除");
                //数据更新
                Model.getInstance().getDbManager().getContactTableDao().deleteContactByHxId(username);
                Model.getInstance().getDbManager().getInviteTableDao().removeInviteStatus(username);
                //发送联系人变化广播
                instance.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
            }

            @Override
            public void onContactInvited(String username, String reason) {
            //接收到联系人新邀请
                Log.e(TAG, "onContactInvited: 联系人邀请");
                //数据库更新
                InviteInfo invitaInfo = new InviteInfo();
                invitaInfo.setUser(new UserInfo(username));
                invitaInfo.setReason(reason);
                 invitaInfo.setStatus(InviteInfo.InvationStatus.NEW_INVITE);
                Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitaInfo);
                //红点的处理
                SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
                //发送邀请信息变化的广播
                instance.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
                //别人同意了你的好友邀请

            }

            @Override
            public void onFriendRequestAccepted(String username) {
            //别人同意了你的请求
                Log.e(TAG, "onFriendRequestAccepted: 对方接受了你的好友请求" );
                //数据库更新

                InviteInfo info = new InviteInfo();
                info.setUser(new UserInfo(username));
                info.setReason("对方接受了你的好友请求");
                info.setStatus(InviteInfo.InvationStatus.INVITE_ACCEPT_BY_PEER);//别人同意了你的添加请求
                Model.getInstance().getDbManager().getInviteTableDao().addInvitation(info);
                //处理红点
                SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
                //发送邀请信息变化的广播
                instance.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
            }

            @Override
            public void onFriendRequestDeclined(String username) {
        //别人拒绝了你的添加请求
                //处理红点
                SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
                //发送邀请信息变化的广播
                instance.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
            }
        });


        EMClient.getInstance().groupManager().addGroupChangeListener(mGroupChangeListener);
    }

    /**
     * 注册一个群消息变化的广播
     */
    private EMGroupChangeListener mGroupChangeListener = new EMGroupChangeListener() {
        @Override
        public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
            //收到群邀请

            //数据更新
            InviteInfo invitationInfo = new InviteInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,inviter));
            invitationInfo.setStatus(InviteInfo.InvationStatus.NEW_GROUP_INVITE);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送广播
            instance.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        @Override
        public void onRequestToJoinReceived(String groupId, String groupName, String applicant, String reason) {
            //收到群申请通知
            InviteInfo invitationInfo = new InviteInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,applicant));
            invitationInfo.setStatus(InviteInfo.InvationStatus.NEW_GROUP_APPLICATION);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送广播
            instance.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        @Override
        public void onRequestToJoinAccepted(String groupId, String groupName, String accepter) {
            //收到 群申请被接受
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送广播
            instance.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        @Override
        public void onRequestToJoinDeclined(String groupId, String groupName, String decliner, String reason) {
            //群申请被拒绝
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送广播
            instance.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        @Override
        public void onInvitationAccepted(String groupId, String invitee, String reason) {
                //收到 群邀请被同意
        }

        @Override
        public void onInvitationDeclined(String groupId, String invitee, String reason) {
            //收到 群邀请被拒绝
        }

        @Override
        public void onUserRemoved(String groupId, String groupName) {
        //群成员被删除

        }

        @Override
        public void onGroupDestroyed(String groupId, String groupName) {
        //收到群邀请被自动接受
        }

        @Override
        public void onAutoAcceptInvitationFromGroup(String groupId, String inviter, String inviteMessage) {

        }

        @Override
        public void onMuteListAdded(String groupId, List<String> mutes, long muteExpire) {

        }

        @Override
        public void onMuteListRemoved(String groupId, List<String> mutes) {

        }

        @Override
        public void onWhiteListAdded(String groupId, List<String> whitelist) {

        }

        @Override
        public void onWhiteListRemoved(String groupId, List<String> whitelist) {

        }

        @Override
        public void onAllMemberMuteStateChanged(String groupId, boolean isMuted) {

        }

        @Override
        public void onAdminAdded(String groupId, String administrator) {

        }

        @Override
        public void onAdminRemoved(String groupId, String administrator) {

        }

        @Override
        public void onOwnerChanged(String groupId, String newOwner, String oldOwner) {

        }

        @Override
        public void onMemberJoined(String groupId, String member) {

        }

        @Override
        public void onMemberExited(String groupId, String member) {

        }

        @Override
        public void onAnnouncementChanged(String groupId, String announcement) {

        }

        @Override
        public void onSharedFileAdded(String groupId, EMMucSharedFile sharedFile) {

        }

        @Override
        public void onSharedFileDeleted(String groupId, String fileId) {

        }
    };
}
